package com.linkdood.app.web.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linkdood.app.domain.Organization;
import com.linkdood.app.domain.ThirdParty;
import com.linkdood.app.service.impl.DdioSynchronize;
import com.linkdood.app.service.OrganizationService;
import com.linkdood.app.service.ThirdPartyService;
import com.linkdood.app.utils.PaginationUtil;
import com.linkdood.app.utils.ResourceUtil;
import com.linkdood.app.web.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * dood 组织
 *
 * @author YunJ
 */
@Api(value = "Dood 组织接口", tags = {"Dood 组织接口"})
@Slf4j
@RestController
@RequestMapping("/api/organization")
public class OrganizationResource {

    private final OrganizationService organizationService;

    private final DdioSynchronize ddioSynchronize;

    private final ThirdPartyService thirdPartyService;

    public OrganizationResource(OrganizationService organizationService, DdioSynchronize ddioSynchronize, ThirdPartyService thirdPartyService) {
        this.organizationService = organizationService;
        this.ddioSynchronize = ddioSynchronize;
        this.thirdPartyService = thirdPartyService;
    }


    @ApiOperation(value = "获取指定 id 的组织信息", notes = "该组织信息为 Dood 服务器的组织信息")
    @ApiImplicitParams( {@ApiImplicitParam(paramType = "query", name = "id", value = "组织 id", required = true, dataType = "String")})
    @GetMapping("/{id}")
    public ResponseEntity<Organization> getOrganization(@PathVariable Long id) {
        return ResponseUtil.wrapOrNotFound(organizationService.getOrganization(id));
    }

    @ApiOperation(value = "获取所有的组织信息", notes = "该组织信息为 Dood 服务器的组织信息, 支持分页")
    @GetMapping()
    public ResponseEntity<?> getOrganizations(@RequestParam(required = false, value = "orgID") String orgId,
                                              Pageable pageable) {
        String thirdPartyId = "";
        if (StringUtils.isNotBlank(orgId)) {
            thirdPartyId = thirdPartyService.getDefaultThirdParty().getId();
        }
        IPage<Organization> page = organizationService.pageOrganizationsByOrgId(thirdPartyId, orgId, PaginationUtil.getPage(pageable));
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page);
        return new ResponseEntity<>(page.getRecords(), headers, HttpStatus.OK);
    }

    /**
     * 同步 organization 信息
     */
    @PreAuthorize("@ss.hasPermi('bxy:ddio-setting:orgSyncData')")
    @ApiOperation(value = "同步 organization 信息", notes = "同步组织机构信息信息")
    @GetMapping("/sync")
    public ResponseEntity<?> syncOrganization(@RequestParam Integer type) {
        ThirdParty thirdParty = thirdPartyService.getDefaultThirdParty();
        ddioSynchronize.syncOrg(type, thirdParty);
        return ResponseEntity.ok(ResourceUtil.success());
    }
}
